import java.io.*;

/**
 * @ClassName Response
 * @Deseription TODO
 * @Author fx
 * @Date 2021/1/10 0010 16:21
 * @Version 1.0
 **/
public class Response {

    private OutputStream outputStream;

    public OutputStream getOutputStream() {
        return outputStream;
    }

    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public Response() {
    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    /**
     * @Author fx
     * @Description 输出指定字符串
     * @ClassName Response
     * @Date 16:25 2021/1/10 0010
     * @Param
     * @return
     **/
    public void output(String content) throws IOException {
        outputStream.write(content.getBytes());
    }


    public void outputHtml(String path) throws IOException {
        // 获取静态资源的绝对路径
        String absoluteResourcePath = StaticResourceUtil.getAbsolutePath(path);

        File file = new File(absoluteResourcePath);
        if(file.exists() && file.isFile()){
            //读取静态文件，输出静态资源
            StaticResourceUtil.outputStaticResource(new FileInputStream(file),outputStream);
        }else {
            // 输出404
            output(HttpProtocolUtil.getHead404());
        }
    }
}
